import { MediaMessageContent, MessageTask, TaskStatus } from "wukongimjssdk";
import config from "../../config";
/**
 * 自定义上传任务 
 */
export class MediaMessageUploadTask extends MessageTask {
  private _progress?: number;

  async start(): Promise<void> {
    const mediaContent = this.message.content as MediaMessageContent;
    if (mediaContent.file) {
      const uploadURL = this.getUploadURL("");
      this.uploadFile(mediaContent.file, uploadURL);
    } else {
      console.log("多媒体消息不存在附件！");
      if (mediaContent.remoteUrl && mediaContent.remoteUrl !== "") {
        this.status = TaskStatus.success;
        this.update();
      } else {
        this.status = TaskStatus.fail;
        this.update();
      }
    }
  }

  async uploadFile(file: File, uploadURL: string) {
    console.warn("上传文件", file, uploadURL);
    const param = new FormData();
    param.append("file", file);
    const resp = uni.uploadFile({
      url: uploadURL,
      filePath: file.path,
      name: "file",
      fileType: "image",
      header: {
        Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
        // "Kingon-Auth":
        //   "bearer " +
        //   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJ0ZW5hbnRfaWQiOiIwMDAwMDAiLCJ1c2VyX25hbWUiOiJhZG1pbiIsInJlYWxfbmFtZSI6IueuoeeQhuWRmCIsInRva2VuX3R5cGUiOiJhY2Nlc3NfdG9rZW4iLCJyb2xlX25hbWUiOiJhZG1pbmlzdHJhdG9yIiwicG9zdF9pZCI6IjExMjM1OTg4MTc3Mzg2NzUyMDEiLCJ1c2VyX2lkIjoiMTEyMzU5ODgyMTczODY3NTIwMSIsInJvbGVfaWQiOiIxMTIzNTk4ODE2NzM4Njc1MjAxIiwibmlja19uYW1lIjoi566h55CG5ZGYIiwiZGV0YWlsIjp7InR5cGUiOiJ3ZWIifSwiZGVwdF9pZCI6IjExMjM1OTg4MTM3Mzg2NzUyMDEiLCJhY2NvdW50IjoiYWRtaW4iLCJjbGllbnRfaWQiOiJraW5nb24iLCJleHAiOjE3MzUyODI0ODAsIm5iZiI6MTczNTI3ODg4MH0.1RzVfaYv1ez-jOYWtXgXRJ61G_lk7TVhk6GFRrrXsUQ",
      },
      success: (uploadRes) => {
        console.log(uploadRes, "||", uploadRes.data, "||", uploadRes.data?.code);
        let data = {};
        if (typeof uploadRes.data === "string") {
          data = JSON.parse(uploadRes.data);
        }
        if (data.success) {
          const mediaContent = this.message.content as MediaMessageContent
          mediaContent.remoteUrl = data.data.link;
          mediaContent.url = data.data.link;
          this.status = TaskStatus.success;
          this.update();
        } else {
          console.error("上传失败", data);
          this.status = TaskStatus.fail;
          this.update();
        }
      },
      fail: (e) => {
        console.error("e", e);
        this.status = TaskStatus.fail;
        this.update();
      },
    });
    if (resp) {
      // 上传进度监听回调
      resp.onProgressUpdate((res) => {
        console.log("res", res);
        this._progress = res.progress;
        // this.status = TaskStatus.success;
        // this.update();
      });
    }
  }

  // 获取上传路径
  getUploadURL(path: string): string {
    const uplaodApi = config.fileUploadUrl;
    if (process.env.NODE_ENV === "development") {
      return import.meta.env.VITE_PREFIX_API + uplaodApi;
    }
    return import.meta.env.VITE_BASE_API + import.meta.env.VITE_PREFIX_API + uplaodApi;
  }

  // 请求暂停
  suspend(): void {}
  // 请求恢复
  resume(): void {}
  // 请求取消
  cancel(): void {
    this.status = TaskStatus.cancel;
    this.update();
  }
  progress(): number {
    return this._progress ?? 0;
  }
}
